<template>
	<view class="">
		<view class="nav_top-bg">
			<u-navbar title="身份信息">
				<view class="u-nav-slot" slot="left">
					<u-icon name="arrow-left" @click="go_black" size="19"></u-icon>
					<u-line direction="column" :hairline="false" length="16" margin="0 10px"></u-line>
					<u-icon name="home" @click="goHome" size="20"></u-icon>
				</view>
			</u-navbar>
		</view>
		<view class="">
			<view class="avatar">
				<text class="touxtxt">姓名</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="请输入姓名" v-model="nickname">
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">电话</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="请输入电话" v-model="phone">
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">身份证号</text>
				<view class="righttx">
					<input class="ipt" type="text" placeholder="身份证号" v-model="card_num">
				</view>
			</view>
			<view class="avatar">
				<text class="touxtxt">地区</text>
				<view class="righttx d-flex align-items" @tap="chooseCity()">
					<view :class="adress == '请选择' ? 'top-txt' : 'top-txt_xz'">{{adress}}</view>
					<image class="qm" src="../../static/index/more.png" mode="widthFix"></image>
				</view>
			</view>
			<view class="w-690 margin-top-30">
				<view class="jieshao_t">介绍</view>
				<view class="">
					<textarea name="" class="xinxi_content" v-model="jieshao" placeholder="请输入个人/公司介绍" id=""
						cols="30" rows="10"></textarea>
				</view>
			</view>
			<view class="upload_zhizhao">
				<text class="zhizhao_tit">主页背景图</text>
				<view class="righttx">
					<view class="adress_suozai">
						<u-upload width="127rpx" height="127rpx" :fileList="fileList3" @afterRead="afterRead" @delete="deletePic" name="3" multiple
							:maxCount="1">
							<image :src="src" class="shangchuanimg" v-if="avatar"></image>
							<image src="../static/upload.png" class="shangchuanimg" v-else></image>
						</u-upload>
					</view>
				</view>
			</view>
		</view>
		<view class="btn">
			<view class="btn-foot">
				<u-button text="确定提交" @click="submit" color="#FF6400"></u-button>
			</view>
		</view>
		<!-- 选择地区 -->
		<u-picker :show="showcity" ref="uPicker" @cancel="showcity = false" :columns="columns_city"
			@confirm="confirm_city" @change="changeHandler" keyName="t_name"></u-picker>
	</view>
</template>

<script>
	import {
		tree
	} from '@/api/citys.js'
	import {
		upFile
	} from '@/utils/url.js'
	import {
		saveXtUserInfo,
		getGroupUserinfo
	} from '@/api/user.js'
	export default {
		data() {
			return {
				nickname: '',
				phone: '',
				card_num: '',
				adress: '请选择',
				cityname: '',
				shiname: '',
				quname: '',
				shengid: '',
				shiid: '',
				quid: '',
				user: {},
				jieshao:'',
				avatar: '',
				//域名头像
				full_avatar: '',
				fileList3:[],
				show: false,
				group_id: '',
				showcity: false,
				columns_city: [],
			}
		},
		async onLoad(option) {
			this.group_id = option.id
			await this.getProvinces()
			this.get_juse_info()
		},
		methods: {
			// 获取角色信息
			get_juse_info() {
				var data = {
					group_id: this.group_id
				}
				getGroupUserinfo(data).then(res => {
					console.log("获取身份信息有没有填写", res);
					if (res.code == 1) {
						this.nickname = res.data.user_groupinfo.name
						this.phone = res.data.user_groupinfo.phone
						this.card_num = res.data.user_groupinfo.card
						this.adress = res.data.user_groupinfo.provincename + '/' + res.data.user_groupinfo
							.cityname + '/' + res.data.user_groupinfo.areaname
						this.shengid = res.data.user_groupinfo.province
						this.shiid = res.data.user_groupinfo.city
						this.quid = res.data.user_groupinfo.area
						this.jieshao = res.data.user_groupinfo.instr
						this.fileList3 = [{
							url: userData.company_bg_image_url,
							url_noyuming: userData.company_bg_image,
						}]
					}
				})
			},
			go_black() {
				uni.navigateBack({
					delta: 1
				})
			},
			goHome() {
				uni.switchTab({
					url: '/pages/index/index'
				})
			},

			chooseCity() {
				console.log("eeeee", this.columns_city);
				this.showcity = true
			},
			// 省市区
			changeHandler(e) {
				const {
					columnIndex,
					value,
					values,
					index,
					indexs,
					picker = this.$refs.uPicker
				} = e
				if (columnIndex === 0) {
					picker.setColumnValues(1, this.columns_city[0][index].children)
					picker.setColumnValues(2, this.columns_city[0][index].children[0].children)
				}
				if (columnIndex === 1) {
					console.log(e, indexs);
					// picker为选择器this实例，变化第二列对应的选项
					picker.setColumnValues(2, this.columns_city[0][indexs[0]].children[indexs[1]].children)
				}
				console.log("columnIndex", columnIndex);
			},
			confirm_city(e) {
				this.shengid = e.value[0].id
				this.shiid = e.value[1].id
				this.quid = e.value[2].id
				this.cityname = e.value[0].name
				this.shiname = e.value[1].name
				this.quname = e.value[2].name
				this.adress = this.cityname + '/' + this.shiname + '/' + this.quname
				this.showcity = false
			},

			// 获取省市县接口数据
			async getProvinces() {
				var data = {
					pid: 0
				}
				await tree(data).then(res => {
					console.log("选择了第几个", res);
					if (res.code == 1) {
						this.columns_city[0] = res.data
						this.columns_city[1] = res.data[0].children
						this.columns_city[2] = res.data[0].children[0].children
					}

				})
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				var url = event.file[0].url
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				console.log("00000000000000", lists);
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						fullurl: result.fullurl,
						url_noyuming: result.url
					}))
					fileListLen++
				}
			},
			uploadFilePromise(url) {
				var that = this
				console.log("url有没有值", url);
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: upFile,
						filePath: url,
						name: 'file',
						formData: {
							user: 'test',
							token: uni.getStorageSync('token')
						},
						success: (res) => {
							console.log("s111111111111111______", res);
							var data = JSON.parse(res.data);
							resolve(data.data)
						}
					});
				})
			},
			// 提交
			submit() {
				var data = {
					name: this.nickname,
					phone: this.phone,
					card: this.card_num,
					province: this.shengid,
					city: this.shiid,
					company_bg_image:this.fileList3.length ? this.fileList3[0].url_noyuming : '', 
					area: this.quid,
					group_id: this.group_id,
					instr: this.jieshao,
				}
				saveXtUserInfo(data).then(res => {
					console.log("保存数据成功", res);
					if (res.code == 1) {
						uni.setStorageSync('shenfen_id',res.code)
						uni.showToast({
							title: '已提交审核，请耐心等待!',
							icon: 'none'
						})
						setTimeout(() => {
							uni.navigateBack({
								delta: 1
							})
						}, 500)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				})
			},
		}
	}
</script>

<style>
	.shangchuanimg {
		width: 127rpx;
		height: 127rpx;
		border: 1px solid #eee;
		display: block;
		margin: auto;
		overflow: hidden;
	}
	.zhizhao_tit{
		font-size: 30rpx;
		font-weight: normal;
		display: block;
		color: #333333;
		margin: 30rpx 0;
	}
	.upload_zhizhao{
		width: 690rpx;
		margin: auto;
	}
	.jieshao_t{
		font-size: 28rpx;
		color: #333;
	}
	.xinxi_content {
		width: 650rpx;
		height: 160rpx;
		background-color: #F5F5F5;
		border-radius: 12rpx;
		margin: auto;
		font-size: 24rpx;
		color: #000000;
		margin-top: 30rpx;
		padding: 20rpx;
	}
	.btn {
		width: 750rpx;
		position: fixed;
		bottom: 40rpx;
		z-index: 10;
	}

	.btn-foot {
		width: 690rpx;
		margin: auto;
	}

	.top-txt_xz {
		font-size: 30rpx;
		color: #333;
	}

	.top-txt {
		font-size: 30rpx;
		color: #999999;
	}

	.youcejt {
		width: 12rpx;
		display: block;
		margin-left: 20rpx;
	}

	.ipt {
		font-size: 30rpx;
		font-weight: normal;
		text-align: right;
		color: #333333;
	}

	.qm {
		width: 12rpx;
		display: block;
		margin-left: 10rpx;
	}

	.righttx {
		display: flex;
		align-items: center;
	}

	.touxtxt {
		font-size: 30rpx;
		font-weight: normal;
		display: block;
		color: #333333;
	}

	.avatar {
		width: 690rpx;
		border-bottom: 1px solid #f5f5f5;
		height: 110rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		margin: auto;
	}
</style>